package org.ezplatform.office.meeting.memo.web.rest;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;

import org.ezplatform.cloud.api.component.popuser.PopUserScopeUtilService;
import org.ezplatform.cloud.api.system.user.UserApiService;
import org.ezplatform.cloud.common.controller.BaseController;
import org.ezplatform.core.web.view.OperationResult;
import org.ezplatform.mobile.util.common.service.CommonService;
import org.ezplatform.office.meeting.common.util.MeetingSendMsgUtils;
import org.ezplatform.office.meeting.common.util.MeetingUtils;
import org.ezplatform.office.meeting.info.entity.ApplyRecord;
import org.ezplatform.office.meeting.info.entity.MeetingScope;
import org.ezplatform.office.meeting.info.service.ApplyRecordDingService;
import org.ezplatform.office.meeting.info.service.ApplyRecordService;
import org.ezplatform.office.meeting.info.service.MeetingScopeService;
import org.ezplatform.office.meeting.memo.entity.MemoInfo;
import org.ezplatform.office.meeting.memo.entity.MemoUserInfo;
import org.ezplatform.office.meeting.memo.service.MemoInfoService;
import org.ezplatform.office.meeting.memo.service.MemoUserInfoService;
import org.ezplatform.util.DateUtils;
import org.ezplatform.util.JsonUtils;
import org.ezplatform.util.StringUtils;
import org.ezplatform.util.UuidUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

/**
 * 
 *会议管理--会议纪要
 *
 */
@RestController("memosfac")
@RequestMapping("api/meeting/memoInfo")
public class MemoInfoApiController extends BaseController {

    private final Logger logger = LoggerFactory.getLogger(MemoInfoApiController.class);
    
    @Autowired
    private MemoInfoService memoInfoService;
    
    @Autowired
    private MemoUserInfoService memoUserInfoService;

    @Autowired
    private ApplyRecordDingService applyRecordDingService;
    
    @Autowired
    private ApplyRecordService applyRecordService;
    
    @Autowired
    private MeetingSendMsgUtils meetingSendMsgUtils;
    
    @Autowired
    private PopUserScopeUtilService popUserScopeUtilService;
    
    @Autowired
    private UserApiService userApiService;
    
    @Autowired
    private MeetingScopeService meetingScopeService;
    
    @Autowired
	private CommonService commonService;
    
    @Autowired
    private MeetingUtils meetingUtils;
	
    @ApiOperation(value = "实体类是否存在", notes = "实体类是否存在")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "id", value = "会议纪要ID", required = true, paramType = "query", dataType = "String") })
	@RequestMapping(value = "/entityIsExist", method = RequestMethod.POST, produces = {"application/json; charset=UTF-8" })
	public OperationResult entityIsExist(String id) {
    	MemoInfo entity = this.memoInfoService.findOne(id);
		if(entity == null) return OperationResult.buildSuccessResult("notExist");
		else return OperationResult.buildSuccessResult("exist");
	}
    
    /**
     * 会议纪要
     */
    @ApiOperation(value="会议纪要", notes="查询会议纪要list列表数据")
    @ApiImplicitParams({
    	@ApiImplicitParam(name = "userId", value = "当前用户ID", required = true, paramType = "query", dataType = "String"),
    	@ApiImplicitParam(name = "corpId", value = "单位ID", required = true, paramType = "query", dataType = "String"),
        @ApiImplicitParam(name = "page", value = "当前显示页数", required = true, paramType = "query", dataType = "String"),
        @ApiImplicitParam(name = "pageSize", value = "一页显示数", required = true, paramType = "query", dataType = "String"),
        @ApiImplicitParam(name = "condition", value = "查询条件", required = true, paramType = "query", dataType = "String"),
        @ApiImplicitParam(name = "type", value = "0:未读   1:已读", required = true, paramType = "query", dataType = "String")
    })
    @RequestMapping(value = "/memoInfoList", method = RequestMethod.GET,produces={"application/json; charset=UTF-8"})
    public OperationResult getMemoInfoList(HttpServletRequest request) {
    	Map<String,Object> map = new HashMap<String,Object>();
        try{
            //当前页
            String currentPage = request.getParameter("page");
            //一页显示条数
            String pageSize = request.getParameter("pageSize");
            //查询条件
            String condition = request.getParameter("condition");
            //0未读、1已读
            String type = request.getParameter("type");
            String userId = request.getParameter("userId");
            String corpId = request.getParameter("corpId");
            String nowDateTime = DateUtils.formatDate(DateUtils.now(),"yyyy-MM-dd HH:mm");
            //查询会议纪要列表数据
            List<Map> list = this.memoInfoService.getMemoInfoList(currentPage, pageSize,userId,corpId,"0",condition,type);
            //查询会议申请列表数count
            //int approvalCount = this.noticeInfoDingService.getNoticeInfoSetListCount(userId,corpId,"1","",0,nowDateTime);
            //查询未读会议纪要列表数count
            //int memoCount = this.memoInfoService.getMemoInfoListCount(userId, corpId);
            map.put("data", list);
            //map.put("approvalCount", approvalCount);
            //map.put("memoCount", memoCount);
            map.put("currentPage", Integer.parseInt(currentPage));
        }catch(Exception e){
            e.printStackTrace();
            return OperationResult.buildFailureResult("获取失败");
        }
        return OperationResult.buildSuccessResult(map);
    }
    
    /**
     * 会议纪要详细页面数据
     * @param request
     * @return
     */
    /*@ApiOperation(value="会议纪要", notes="会议纪要详细信息")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "id", value = "会议纪要id", required = true, paramType = "query", dataType = "String"),
        @ApiImplicitParam(name = "memoUserInfoId", value = "会议纪要用户记录表id", required = true, paramType = "query", dataType = "String"),
    })
    @RequestMapping(value = "/memoInfo", method = RequestMethod.GET,produces={"application/json; charset=UTF-8"})
    public Map<String,Object> getMemoInfo(HttpServletRequest request) {
        Map<String,Object> result = new HashMap<String,Object>();
        Map<String,Object> map = new HashMap<String,Object>();
        try{
            WebUser user = WebUtils.getCurrentUser();
            String token = user.getJwtToken();
            String corpId = user.getCorpId();
            
            String id = request.getParameter("id");
            String memoUserInfoId = request.getParameter("memoUserInfoId");
            MemoInfo memoInfo = this.memoInfoDingService.findOne(id);
            MemoUserInfo memoUserInfo = this.memoUserInfoDingService.findOne(memoUserInfoId);
            
            //查询会议
            String applyRecordId = memoInfo.getApplyRecordId();
            map = this.applyRecordDingService.getApplyRecordContent(request,applyRecordId,map,memoUserInfo.getMemoUserId(),corpId,token);

            //修改状态
            memoUserInfo.setIsView(1);
            this.memoUserInfoDingService.save(memoUserInfo);
            
            map.put("memoInfo", memoInfo);
            result.put("success", true);
            result.put("content",map );
            
        }catch(Exception e){
            result.put("success", false);
            result.put("content", "");
        }
        return result;
    }*/
    
    /**
     * 新建、编辑会议纪要
     * @param request
     * @return
     */
    @ApiOperation(value="会议纪要", notes="新建、编辑会议纪要")
    @ApiImplicitParams({
    	@ApiImplicitParam(name = "userId", value = "当前用户ID", required = true, paramType = "query", dataType = "String"),
    	@ApiImplicitParam(name = "corpId", value = "单位ID", required = true, paramType = "query", dataType = "String"),
    	@ApiImplicitParam(name = "token", value = "token", required = true, paramType = "query", dataType = "String"),
        @ApiImplicitParam(name = "id", value = "会议纪要id", required = true, paramType = "query", dataType = "String"),
        @ApiImplicitParam(name = "isViewType", value = "查看或编辑", required = true, paramType = "query", dataType = "String"),
        @ApiImplicitParam(name = "applyRecordId", value = "会议id", required = true, paramType = "query", dataType = "String"),
        @ApiImplicitParam(name = "cnName", value = "用户中文名", required = true, paramType = "query", dataType = "String"),
        @ApiImplicitParam(name = "userName", value = "用户名", required = true, paramType = "query", dataType = "String"),
    })
    @RequestMapping(value = "/editMemoInfo", method = RequestMethod.GET,produces={"application/json; charset=UTF-8"})
    public OperationResult editMemoInfo(HttpServletRequest request) {
        Map<String,Object> map = new HashMap<String,Object>();
        try{
            String corpId = request.getParameter("corpId");
            String userId = request.getParameter("userId");
            String token = request.getParameter("token");
            String id = request.getParameter("id");
            String applyRecordId = request.getParameter("applyRecordId");
            String isViewType = request.getParameter("isViewType");
            String cnName = request.getParameter("cnName");
            String userName = request.getParameter("userName");
            MemoInfo memoInfo = null;
            Object attIdJson = null;
            if(StringUtils.isNotEmpty(id)){
                memoInfo = this.memoInfoService.findOne(id);
                if(memoInfo==null){
                	map.put("isDeleted", true);
                	return OperationResult.buildSuccessResult(map);
                }else{
                	map.put("isDeleted", false);
                    if(isViewType.equals("1")){
                        MemoUserInfo memoUserInfo = this.memoUserInfoService.getMemoUserInfo(id, userId, corpId);
                        if(memoUserInfo!=null){
                            //修改状态
                            memoUserInfo.setIsView(1);
                            this.memoUserInfoService.save(memoUserInfo);
                        }
                    }
                }
                attIdJson = memoInfoService.transformAttachmentPlanform2Dd(StringUtils.null2String(memoInfo.getFileId()), token);
            }else{
                memoInfo = new MemoInfo();
            }
            //查询会议
            map = this.applyRecordDingService.getMemoApplyRecordContent(applyRecordId,corpId,token);
            ApplyRecord applyRecord = (ApplyRecord)map.get("applyRecord");
            map.put("isCreatedBy", userId.equals(StringUtils.null2String(applyRecord.getCreatedBy())));
            //主持人
            boolean isEmceeUser = false;
			List<String> eScopeList = new ArrayList<>();
			eScopeList.add(applyRecord.getEmceeUser());
			List<MeetingScope> emceeUserScopeList = this.meetingScopeService.getMeetingScopeList("oa_meeting_apply_record", "emcee_user", eScopeList,true);
			if(emceeUserScopeList!=null && emceeUserScopeList.size()>0){
				for(MeetingScope m : emceeUserScopeList){
					String scopeValue = m.getScopevalue();
					if(scopeValue.equals(userId)){
						isEmceeUser = true;
					}
				}
			}
			map.put("isEmceeUser", isEmceeUser);
			//纪要员
			boolean isMemoUser = false;
			List<String> mScopeList = new ArrayList<>();
			mScopeList.add(applyRecord.getMemoUser());
			List<MeetingScope> memoUserScopeList = this.meetingScopeService.getMeetingScopeList("oa_meeting_apply_record", "memo_user", mScopeList,true);
			if(memoUserScopeList!=null && memoUserScopeList.size()>0){
				for(MeetingScope m : memoUserScopeList){
					String scopeValue = m.getScopevalue();
					if(scopeValue.equals(userId)){
						isMemoUser = true;
					}
				}
			}
            map.put("isMemoUser", isMemoUser);
            map.put("memoInfo", memoInfo);
            map.put("attIdJson", attIdJson);
            map.put("fileIds", StringUtils.null2String(memoInfo.getFileId()));
            String sysWatermark = commonService.getSysParamValueByMark(corpId, token, "sysWatermark");
            if(StringUtils.isNotBlank(sysWatermark) && sysWatermark.contains("meetingMemo")) {
            	map.put("watermarkContent", cnName + userName);
            	map.put("watermark", "1");
            }else {
            	map.put("watermark", "1");
            	map.put("watermarkContent", cnName + userName);
            }
        }catch(Exception e){
        	return OperationResult.buildFailureResult("获取失败");
        }
        return OperationResult.buildSuccessResult(map);
    }
    
    @ApiOperation(value = "保存会议纪要", notes = "保存会议纪要")
    @ApiImplicitParams({
    	@ApiImplicitParam(name = "userId", value = "当前用户ID", required = true, paramType = "query", dataType = "String"),
    	@ApiImplicitParam(name = "userName", value = "当前用户名", required = true, paramType = "query", dataType = "String"),
    	@ApiImplicitParam(name = "org", value = "组织id", required = true, paramType = "query", dataType = "String"),
    	@ApiImplicitParam(name = "corpId", value = "单位ID", required = true, paramType = "query", dataType = "String"),
    	@ApiImplicitParam(name = "token", value = "token", required = true, paramType = "query", dataType = "String"),
    	@ApiImplicitParam(name = "message", value = "保存会议纪要的参数", required = true, paramType = "body", dataType = "String") 
    })
    @RequestMapping(value = "/saveMemoInfo", method = RequestMethod.POST)
    public OperationResult saveMemoInfo(@RequestBody String message,HttpServletRequest request) {
        logger.debug("保存会议纪要开始");
        try {
        	String token = request.getParameter("token");
        	String userId = request.getParameter("userId");
        	String userName = request.getParameter("userName");
        	String corpId = request.getParameter("corpId");
        	String userOrg = request.getParameter("org");
			Map<String, Object> map = JsonUtils.readValue(message);
			
            MemoInfo memoInfo = null;
            String id = StringUtils.null2String(map.get("id"));
            if (StringUtils.isNotEmpty(id)) {
                memoInfo = this.memoInfoService.findOne(id);
                if (memoInfo == null) {
                    memoInfo = new MemoInfo();
                }
            } else {
                memoInfo = new MemoInfo();
            }
            if("".equals(StringUtils.null2String(memoInfo.getId()))){
            	memoInfo.setCreatedBy(userId);
            	memoInfo.setCorpId(corpId);
            }else{
            	memoInfo.setLastModifiedBy(userId);
            }
            
            String applyRecordId = StringUtils.null2String(map.get("applyRecordId"));
            memoInfo.setContent(StringUtils.null2String(map.get("content")));
            memoInfo.setAgenda(StringUtils.null2String(map.get("agenda")));
            memoInfo.setTasks(StringUtils.null2String(map.get("tasks")));
            memoInfo.setApplyRecordId(applyRecordId);
            memoInfo.setFileId(StringUtils.null2String(map.get("fileId")));
            String memoUserJsonStr = StringUtils.null2String(map.get("jsonStrMemoUser"));
            memoInfo.setMemoUser(saveScope(memoUserJsonStr, StringUtils.null2String(memoInfo.getMemoUser()), "memo_scope"));
            memoInfo.setShowWatermark(StringUtils.null2String(map.get("showWatermark")));
            memoInfo = this.memoInfoService.save(memoInfo);
            
            ApplyRecord applyRecord = this.applyRecordService.findOne(applyRecordId);
            List<String> userIdList = this.memoInfoService.getMemoInfoUserList(applyRecord, memoInfo, token);
            //编辑时需要检测纪要通知的人有没有增减
            List<String> addList = new ArrayList<String>();
            if(StringUtils.isNotEmpty(id)){
            	List<MemoUserInfo> memoUserInfoList = memoUserInfoService.getMemoUserInfoByMemoId(id, corpId);
    			List<MemoUserInfo> deleteList = new ArrayList<MemoUserInfo>();
    			List<String> ids = new ArrayList<String>();
    			for(MemoUserInfo mui : memoUserInfoList) {
    				//老的id不在新的id集合里面，说明已删除
    				if(!userIdList.contains(mui.getMemoUserId())) {
    					deleteList.add(mui);
    				}
    				ids.add(mui.getMemoUserId());
    			}
    			for(String _userId : userIdList) {
    				//老的id集合不包含新的id，说明新的id是新增的
    				if(!ids.contains(_userId)) {
    					addList.add(_userId);
    				}
    			}
    			this.memoUserInfoService.delete(deleteList);
    			this.memoUserInfoService.addMemoUserInfo(memoInfo.getId(), addList, corpId);
            }else {
            	addList.addAll(userIdList);
            	this.memoUserInfoService.addMemoUserInfo(memoInfo.getId(), userIdList, corpId);
            	String createdUserId = applyRecord.getCreatedBy();
            	//会议发起人也能发起看见会议纪要
            	if(!addList.contains(createdUserId)) {
                	MemoUserInfo m = new MemoUserInfo();
                    m.setMemoId(memoInfo.getId());
                    m.setMemoUserId(createdUserId);
                    m.setIsView(0);
                    m.setCorpId(corpId);
                    memoUserInfoService.save(m);
            	}
            }
        	Map<String, Object> ds = new HashMap<>();
        	ds.put("type", 5);
    		ds.put("applyRecordId", applyRecord.getId());
    		ds.put("meetingTitle", applyRecord.getName());
    		ds.put("id", memoInfo.getId());
    		ds.put("userId", userId);
    		ds.put("userName", userName);
    		ds.put("userOrg", userOrg);
    		StringBuffer ids = new StringBuffer();
    		StringBuffer jsonIds = new StringBuffer("[");
    		for(int i = 0; i < addList.size(); i++) {
    			if(i == addList.size() - 1) {
    				ids.append(addList.get(i));
    				jsonIds.append("{\"10\":\"" + addList.get(i) + "\"}");
    			}else {
    				ids.append(addList.get(i) + ",");
    				jsonIds.append("{\"10\":\"" + addList.get(i) + "\"},");
    			}
    		}
    		jsonIds.append("]");
    		
    		if(!"".equals(ids.toString())) {
    			boolean mobilePush = false;
                boolean emailPush = false;
                boolean smsPublish = false;
    			String mesgPush = applyRecord.getMessagePush();
    			if(StringUtils.isNotEmpty(mesgPush) && mesgPush.contains("mobile")) mobilePush = true;
    			if(StringUtils.isNotEmpty(mesgPush) && mesgPush.contains("email")) emailPush = true;
    			if(StringUtils.isNotEmpty(mesgPush) && mesgPush.contains("sms")) smsPublish = true;
    			ds.put("userIds", ids.toString());
        		ds.put("orgIds", "");
    			this.meetingSendMsgUtils.sendMail(ds, emailPush, corpId, TOKEN);
    			this.meetingSendMsgUtils.sendMsgToApp(ds, mobilePush, corpId, token, request);
				this.meetingSendMsgUtils.sendSMS(ds, smsPublish, corpId, token);
    		}
    		if(!"[]".equals(ids.toString()))this.meetingSendMsgUtils.sendMessage(ds, jsonIds.toString(), corpId, token);
        } catch (Exception e) {
            e.printStackTrace();
            return OperationResult.buildFailureResult("保存会议纪要失败");
        }
        logger.debug("保存会议纪要结束");
        return OperationResult.buildSuccessResult("保存会议纪要成功");
    }
    
    /**
	 * 选人组件信息保存到scope表
	 * @param jsonStr        json串
	 * @param fieldDataValue entity中的关联scope表的fieldDataValue字段的值
	 * @param fieldData      scope表的fieldData字段的值
	 * @return
	 */
	private String saveScope(String jsonStr, String fieldDataValue, String fieldData) {
		String scopeCode = StringUtils.null2String(fieldDataValue);
		if ("".equals(scopeCode)) {
			scopeCode = UuidUtils.UUID();
		}
		this.popUserScopeUtilService.saveScopeData("oa_meeting_apply_record_scope", scopeCode, jsonStr, "oa_meeting_memo_info", fieldData);
		return scopeCode;
	}
    
	private Map<String, String> transformUserScopePlatform2Dd(String fieldDataValue, String fieldData, String corpId, String token){
        String ids = "", jsonIds = "";
        Map<String, String> result = new HashMap<>();
        String scopeTypeStr = this.popUserScopeUtilService.getScopeTypeValue("oa_meeting_apply_record_scope", "oa_meeting_apply_record", fieldData, StringUtils.null2String(fieldDataValue));
        String[] scopeTypeArray = scopeTypeStr.split(",");
        if(scopeTypeArray != null && scopeTypeArray.length>0) {
        	List<String> l = new ArrayList<>();
            for (int i = 0; i < scopeTypeArray.length; i++) {
                String[] info = scopeTypeArray[i].split("SCOPE");
                if (info.length > 1){
                	String id  = info[1];
                	String type  = info[0];
                	List<String> u = new ArrayList<>();
                	if("user".equals(type)){
                        jsonIds +="{\"10\":\""+id+"\"},";
                        l.add(id);
//                		u = this.userApiService.getUserIdBySysOrThird(id, "1", "DD", corpId, token);
                	}
//                    if(u!=null && u.size()>0){
//                    	l.addAll(u);
//                    }
                }
            }
            if(l!=null && l.size()>0){
            	for(int i=0; i<l.size(); i++){
            		String thirdId = l.get(i);
            		if(StringUtils.isNotEmpty(thirdId)){
            			ids += thirdId + ",";
            		}
            	}
            }
        }
        result.put("jsonIds", jsonIds);
        result.put("ids", ids);
        return result;
    }
	
	private Map<String, String> transformUOScopePlatform2Dd(String fieldDataValue, String fieldData, String corpId, String token){
		Map<String, String> map = new HashMap<>();
		String ids = "", orgIds = "", jsonIds = "";
        String scopeTypeStr = this.popUserScopeUtilService.getScopeTypeValue("oa_meeting_apply_record_scope", "oa_meeting_apply_record", fieldData, StringUtils.null2String(fieldDataValue));
        String[] scopeTypeArray = scopeTypeStr.split(",");
        if(scopeTypeArray != null && scopeTypeArray.length>0) {
        	List<String> l = new ArrayList<>();
        	List<String> o = new ArrayList<>();
            for (int i = 0; i < scopeTypeArray.length; i++) {
                String[] info = scopeTypeArray[i].split("SCOPE");
                if (info.length > 1){
                	String id  = info[1];
                	String type  = info[0];
                	if("user".equals(type)){
                        jsonIds +="{\"10\":\""+id+"\"},";
                        l.add(id);
//                		List<String> u = this.userApiService.getUserIdBySysOrThird(id, "1", "DD", corpId, token);
//                		if(u!=null && u.size()>0){
//                        	l.addAll(u);
//                        }
                	}else{
                        jsonIds +="{\"20\":\""+id+"\"},";
                        o.add(id);
//                		List<String> u = this.orgApiService.getOrgIdOrThirdUid(id, "1", "DD", corpId, token);
//                		if(u!=null && u.size()>0){
//                            for(int j=0; j<u.size(); j++){
//                                if("1".equals(u.get(j))){
//                                    Map<String, Object> topOrgInfo = this.orgApiService.getPopuOrgChildInfo(id,"user", "","", "", corpId, token);
//                                    List<Map<String, Object>> userList = (List<Map<String, Object>>)topOrgInfo.get("userList");
//                                    List<Map<String, Object>> orgList = (List<Map<String, Object>>)topOrgInfo.get("orgList");
//                                    if(userList!=null && userList.size()>0){
//                                        for(int k=0; k<userList.size(); k++){
//                                            Map<String, Object> map1 = userList.get(k);
//                                            String ddUId = StringUtils.null2String(map1.get("ddUId"));
//                                            if(StringUtils.isNotEmpty(ddUId)){
//                                                l.add(ddUId);
//                                            }
//                                        }
//                                    }
//                                    if(orgList!=null && orgList.size()>0){
//                                        for(int k=0; k<orgList.size(); k++){
//                                            Map<String, Object> map1 = orgList.get(k);
//                                            String ddUId = StringUtils.null2String(map1.get("ddUId"));
//                                            if(StringUtils.isNotEmpty(ddUId)){
//                                                o.add(ddUId);
//                                            }
//                                        }
//                                    }
//                                }else{
//                                    o.add(u.get(j));
//                                }
//                            }
//                        }
                	}
                }
            }
            if(l!=null && l.size()>0){
            	for(int i=0; i<l.size(); i++){
            		String thirdId = l.get(i);
            		if(StringUtils.isNotEmpty(thirdId)){
            			ids += thirdId + ",";
            		}
            	}
            }
            
            if(o!=null && o.size()>0){
            	for(int i=0; i<o.size(); i++){
            		String thirdId = o.get(i);
            		if(StringUtils.isNotEmpty(thirdId)){
            			orgIds += thirdId + ",";
            		}
            	}
            }
        }
        map.put("jsonIds", jsonIds);
        map.put("userIds", ids);map.put("orgIds", orgIds);
        return map;
    }
	
	@ApiOperation(value = "纪要转发功能", notes = "纪要转发功能")
    @ApiImplicitParams({
    	@ApiImplicitParam(name = "token", value = "token", required = true, paramType = "query", dataType = "String"),
    	@ApiImplicitParam(name = "corpId", value = "单位ID", required = true, paramType = "query", dataType = "String"),
        @ApiImplicitParam(name = "memoId", value = "纪要ID", required = true, paramType = "query", dataType = "String"),
        @ApiImplicitParam(name = "jsonStrTransmiScope", value = "转发接收人", required = false, paramType = "body", dataType = "String")
        })
    @RequestMapping(value = "/transmitMemo", method = RequestMethod.POST)
    public OperationResult transmitMemo(HttpServletRequest request) {
    	try {
    		String corpId = request.getParameter("corpId");
            String token = request.getParameter("token");
            String memoId = request.getParameter("memoId");
            String jsonStrTransmiScope = request.getParameter("jsonStrTransmiScope");
			List<String> addList = null;
			if(StringUtils.isNotEmpty(jsonStrTransmiScope)) {
				JSONArray json = (JSONArray) JSONArray.parse(jsonStrTransmiScope);
			    if(json != null) {
			    	String userId = "", orgId = "", type = "";
			    	Set<String> userIdList = new HashSet<String>();
			    	for(int i = 0; i < json.size(); i++) {
			    		JSONObject jo = (JSONObject)json.get(i);
			    		type = jo.getString("scopeType");
			    		if("user".equals(type)) {
			    			userId = jo.getString("scopeValue");
			    			userIdList.add(userId);
			    		}else if("org".equals(type)) {
			    			orgId = jo.getString("scopeValue");
			    			List<String> unitUserList = this.userApiService.getUserIdsByOrgId(orgId, "", "1", corpId, token);
			                if(unitUserList!=null && unitUserList.size()>0){
			                	userIdList.addAll(unitUserList);
			                }
			    		}
			    	}
			    	addList = new ArrayList<String>(userIdList);
			    }
			    this.memoUserInfoService.addMemoUserInfo(memoId, addList, corpId);
			}
			return OperationResult.buildSuccessResult("转发成功");
		} catch (Exception e) {
			e.printStackTrace();
			return OperationResult.buildFailureResult("转发失败");
		}
    }
	
	@ApiOperation(value = "删除会议纪要", notes = "删除会议纪要")
    @ApiImplicitParams({
    	@ApiImplicitParam(name = "corpId", value = "单位ID", required = true, paramType = "query", dataType = "String"),
        @ApiImplicitParam(name = "memoId", value = "纪要ID", required = true, paramType = "query", dataType = "String"),
        @ApiImplicitParam(name = "jsonStrTransmiScope", value = "转发接收人", required = false, paramType = "body", dataType = "String")
        })
    @RequestMapping(value = "/deleteMemoInfo", method = RequestMethod.POST)
    public OperationResult deleteMemoInfo(HttpServletRequest request) {
    	try {
    		String corpId = request.getParameter("corpId");
            String memoId = request.getParameter("memoId");
			List<MemoUserInfo> list = this.memoUserInfoService.getMemoUserInfoByMemoId(memoId, corpId);
			this.memoUserInfoService.delete(list);
			MemoInfo mi = memoInfoService.findOne(memoId);
			this.memoInfoService.delete(mi);
			return OperationResult.buildSuccessResult("删除成功");
		} catch (Exception e) {
			e.printStackTrace();
			return OperationResult.buildFailureResult("删除失败");
		}
    }
}